import { BASE_URL } from '../../apis/requests.js';
// pages/findBorrowedBooks/findBorrowedBooks.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    books: [],
    remain_days: [],
    notes: []
  },

  bindReturn(e) {
    console.log(e.currentTarget.dataset.index)
    console.log(this.data.books[e.currentTarget.dataset.index].bookinfo.book_id)

    const values = {
      "token": wx.getStorageSync('token'),
      "id": this.data.books[e.currentTarget.dataset.index].borrow_user_id,
      "book_id": parseInt(this.data.books[e.currentTarget.dataset.index].bookinfo.book_id)
    }
    console.log(values)

    wx.request({
      url: BASE_URL + "/admin/return_book",
      data: values,
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      dataType: 'json',
      success: (result) => {
        console.log(result)
        if (result.data.code === "200") {
          wx.showModal({
            title: "提示",
            content: "操作成功！",
            showCancel: false,
            success(res) {
              wx.navigateBack({
                complete: (res) => {},
              })
            }
          })

        }

      },
      fail: () => {},
      complete: () => {}
    })   


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this
    const values = {
      "token": wx.getStorageSync('token')
    }

    wx.request({
      url: BASE_URL + "/find_borrow_books",
      data: values,
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      dataType: 'json',
      success: (result) => {
        console.log(result)
        if (result.data.code === "200") {
          this.setData({
            books: result.data.data
          })
          
        for(let index in that.data.books) {
          let dateDiff = this.data.books[index].remain_date
          let note = null
          if (parseInt(dateDiff) >= 0) {
            note = "剩余" + dateDiff + "天"
          } else {
            note = "超期" + -dateDiff + "天"
          }
          console.log(note)

          that.setData({
            remain_days: this.data.remain_days.concat(dateDiff),
            notes: this.data.notes.concat(note)
          })
        }
        } else {
          wx.showModal({
            title: "提示",
            content: "查询失败，请重试！",
            showCancel: false,
            success(res) {}
          })
        }

      },
      fail: () => {},
      complete: () => {}
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})